const express = require("express");
const mysql = require("mysql");
const cors = require("cors");
const session = require("express-session");

var pool = mysql.createPool({
	host: "127.0.0.1",
	user: "root",
	password: "root",
	database: "alunnis",
	pore: "3306"
});
var server = express();
server.use(cors({
	origin: ["https://1705.wcdzn.xyz","http://172.84.11.12:8080", "http://127.0.0.1:8080", "http://localhost:8080", "http://127.0.0.1:80", "http://localhost:80"],
	credentials: true
}));
server.use(session({
	secret: "128位安全字符串",
	resave: true,
	saveUninitialized: true
}));
server.use(express.static("public"));
server.listen(3000);
//查询信息
server.get("/user", (req, res) => {
	var gender = req.query.gender;
	var sql = "select uid,uname,text,avatar,number from 1705_users where gender=? ";
	pool.query(sql, [gender], (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({ code: 1, data: result });
		} else {
			res.send({ code: -1 })
		}
	})
})
// 图pain查找
server.get("/carousel", (req, res) => {
	var obj = req.query;
	var sql = "select img from 1705_carousel where type=?";
	pool.query(sql, [obj.type], (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({ code: 1, data: result });
		} else {
			res.send({ code: -1 })
		}
	})
})
//同学录查找
server.get("/avatar", (req, res) => {
	var uid = req.query.uid;
	var sql = "select uname,avatar from 1705_users where uid=? ";
	pool.query(sql, [uid], (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({ code: 1, data: result });
		} else {
			res.send({ code: -1 })
		}
	})
})
//搜索
server.get("/search", (req, res) => {
	var sql = "select uname from 1705_users";
	pool.query(sql, (err, result) => {
		if (err) throw err;
		if (result.length > 0) {
			res.send({ code: 1, data: result });
		} else {
			res.send({ code: -1 })
		}
	})
})
//  同学录列表
server.get("/morelist", (req, res) => {
	var pno = req.query.pno;
	var ps = req.query.pageSize;
	if (!pno) { pno = 1 };
	if (!ps) { ps = 10 };
	var offset = (pno - 1) * ps;
	ps = parseInt(ps);

	var sql = "SELECT * FROM 1705_liuyan LIMIT ?,?";
	pool.query(sql, [offset, ps], (err, result) => {
		if (err) throw err;
		result = JSON.stringify(result);//把results对象转为字符串，去掉RowDataPacket
		data = JSON.parse(result);//把results字符串转为json对象
			var sql1="select count(aid) as c from 1705_liuyan"
			pool.query(sql1, (err, result) => {
				if (err) throw err;
				var pageCount = Math.ceil(result[0].c / ps);
				var count = result[0].c;
				res.send({ code: 1, data: data,count:count,pageCount:pageCount})
			});
	});
})
// 搜索确认  同学录列表
server.get("/more", (req, res) => {
	var uname = req.query.uname;
	var pno = req.query.pno;
	var ps = req.query.pageSize;
	if (!pno) { pno = 1 };
	if (!ps) { ps = 10 };
	var offset = (pno - 1) * ps;
	ps = parseInt(ps);
	var sql = "SELECT uname,avatar,aid,name,advice FROM 1705_liuyan where uname=? LIMIT ?,?";
	pool.query(sql, [uname,offset, ps], (err, result) => {
		if (err) throw err;
		result = JSON.stringify(result);//把results对象转为字符串，去掉RowDataPacket
		data = JSON.parse(result);//把results字符串转为json对象
			var sql1="select count(aid) as c from 1705_liuyan where uname=?"
			pool.query(sql1,[uanme] ,(err, result) => {
				if (err) throw err;
				var pageCount = Math.ceil(result[0].c / ps);
				var count = result[0].c;
				res.send({ code: 1, data: data,count:count,pageCount:pageCount})
			});
	});
})
//同学录提交
server.get("/submit", (req, res) => {
	var obj = req.query
	var sql = "INSERT INTO 1705_liuyan VALUES (NULL,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ? )";
	pool.query(sql, [obj.name, obj.qq, obj.blood, obj.conste, obj.phone, obj.motto, obj.target, obj.worship, obj.advice, obj.value, obj.style, obj.uid, obj.avatar, obj.uname], (err, result) => {
		if (err) throw err;
		if (result.affectedRows > 0) {
			res.send({ code: 1 });
		} else {
			res.send({ code: -1 })
		}
	})
})